<?php

require_once 'db_connect.php';

// define the query to get an entry for today from the cache table
$iresult = mysql_query("SELECT * FROM institutions");
$mresult = mysql_query("SELECT * FROM institutions AS i, modules AS m WHERE i.institution_id=m.fk_institution_id");
$tresult = mysql_query("SELECT * FROM institutions AS i, modules AS m, tutorials AS t WHERE 
	i.institution_id=m.fk_institution_id AND m.module_id=t.fk_module_id");
$sresult = mysql_query("SELECT * FROM institutions AS i, modules AS m, tutorials AS t, students AS s 
	WHERE i.institution_id = m.fk_institution_id AND m.module_id = t.fk_module_id AND t.tutorial_id = s.fk_tutorial_id");


while ($irow = mysql_fetch_object($iresult)) {
	$list[$irow->institution_name] = array();
}

while ($mrow = mysql_fetch_object($mresult)) {	
	if ($mrow->institution_id == $mrow->fk_institution_id) {
		$list[$mrow->institution_name][$mrow->module_title] = array();
		$mod_id[$mrow->module_title] = $mrow->module_id;
	}
}

while ($trow = mysql_fetch_object($tresult)) {	
	if ($trow->module_id == $trow->fk_module_id) {
		$list[$trow->institution_name][$trow->module_title][$trow->tutorial_title] = array();
	}
}

while ($srow = mysql_fetch_object($sresult)) {	
	if ($srow->t_id == $srow->tid) {
		$list[$srow->institution_name][$srow->module_title][$srow->tutorial_title][$srow->student_id] = $srow->sname.", ".substr($srow->fname, 0, 1);
	}
}

// print_r($mod_id);

$mlist = '';

foreach ($list as $key => $value) {

	$mlist .= '<li class="open"><span class="folder">'.$key.'</span>';
	$mlist .=	'<ul>';
	
	foreach ($value as $key1 => $value1) {
		$mlist .= '<li class="closed"><span id="module" class="folder" value="'.$mod_id["$key1"].'">'.$key1.'</span>';
		$mlist .=	'<ul>';
		
		foreach ($value1 as $module => $tutorial) {
			$mlist .= '<li class="closed"><span class="folder">'.$module.'</span>';
			$mlist .=	'<ul>';
		
			foreach ($tutorial as $student => $name) {
			
				$mlist .= '<li><span class="file"><span id="menuitem"><a id="'.$student.'" href="#">'.$name.'</a></span></span></li>';
			}
			
			$mlist .= '</ul>';
			$mlist .= '</li>';		
	
		}
			
		$mlist .= '</ul>';
		$mlist .= '</li>';
	}

	$mlist .= '</ul>';
	$mlist .= '</li>';

}	

echo $mlist;
       
?>
